<?php

/**
 * 前台 首页 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: index.class.php 1605 2011-03-15 08:16:23Z zhangwentao $
 * http://www.t-mac.org；
 */
class index extends Model
{

    private $links_table;
    private $category_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->links_table = '#@__links';
        $this->category_table = '#@__categories';
    }

    /**
     * 取链接
     * @param <type> $type
     * @param <type> $name
     * @param <type> $limit
     * @return <type>
     */
    public function getFlink($cat_pid, $name = 'flink_array', $limit = 8)
    {
        $time = time();
        /*
          $sql = "SELECT a.*, b.cat_name, b.cat_order FROM $this->links_table a LEFT JOIN $this->category_table b ON a.cat_id = b.cat_id ";
          $sql .= "WHERE IF(a.link_state_radio = 1, a.link_starttime<$time AND a.link_endtime>$time, a.link_state_radio <> 3) ";
          $sql .= "AND b.cat_pid = $cat_pid ";
          $sql .= "ORDER BY a.link_order DESC ";
         */
        $sql = "SELECT a.*, c.cat_name, c.cat_order FROM $this->links_table a,$this->category_table c ";
        $sql .= "WHERE(SELECT count(*) FROM $this->links_table b WHERE IF(b.link_state_radio = 1, b.link_starttime<$time AND b.link_endtime>$time, b.link_state_radio <> 3) AND a.cat_id = b.cat_id AND a.link_id > b.link_id ORDER BY b.link_id ASC ) < 7 ";
        $sql .= "AND a.cat_id=c.cat_id ";
        $sql .= "AND c.cat_pid = $cat_pid ";
        $sql .= "ORDER BY c.cat_id ASC, a.link_id ASC";

        $rs = $this->db->cacheGetAll($sql, 86400);
        $result = array();
        $name_array = array();
        foreach ($rs AS $k => $v) {
            $result[$v['cat_id']][] = $v;
            $name_array[$v['cat_id']]['cat_name'] = $v['cat_name'];
            $name_array[$v['cat_id']]['cat_id'] = $v['cat_id'];
        }
        $this->assign($name, $result);
        $this->assign($name . '_name_array', $name_array);
        return $result;
    }

    /**
     * 取推荐的链接
     * @return <type>
     */
    public function getRecommend()
    {
        $sql = "SELECT cat_id, link_url, link_name, link_target FROM $this->links_table WHERE link_recommend = 1 ORDER BY link_order DESC LIMIT 0,48";
        $result = $this->db->cacheGetAll($sql, 86400);
        $this->assign('recommend_array', $result);
        return $result;
    }

    /**
     * 取分类left
     * @return <type>
     */
    public function getCategory()
    {
        $sql = "SELECT cat_id, cat_pid, cat_name FROM $this->category_table ORDER BY cat_order DESC, cat_id ASC";
        $result = $this->db->cacheGetAll($sql, 86400);
        $rs = array();
        foreach ($result AS $k => $v) {
            if ($v['cat_pid'] != 0) {
                $rs[$v['cat_pid']][] = $v;
            }
        }
        $sql = "SELECT cat_id, cat_pid, cat_name FROM $this->category_table WHERE cat_pid = 0 ORDER BY cat_order DESC, cat_id ASC";
        $cat_result = $this->db->cacheGetAll($sql, 86400);
        foreach ($cat_result AS $k => $v) {
            $cat_result[$v['cat_id']] = $v['cat_name'];
        }
        $this->assign('category_array', $rs);
        $this->assign('category_cat_array', $cat_result);
        return $result;
    }

}